using System.Xml.Linq;
using Engine;
using Game;

namespace SCIENEW {
    public class IERecipaediaRecipesScreen : Screen {

        public IECraftingRecipeWidget m_craftingRecipeWidget;

        public IESmeltingRecipeWidget m_smeltingRecipeWidget;

        public One2OneRecipeWidget m_one2OneRecipeWidget;

        public SiftingRecipeWidget m_siftingRecipeWidget;

        public CokingRecipeWidget m_cokingRecipeWidget;

        public ButtonWidget m_prevRecipeButton;

        public ButtonWidget m_nextRecipeButton;

        public int m_recipeIndex;

        public List<CraftingRecipe> m_craftingRecipes = [];

        public Stack<(List<CraftingRecipe> Recipes, int Index)> RecipesStack = new();

        public IERecipaediaRecipesScreen() {
            XElement node = ContentManager.Get<XElement>("Screens/IERecipaediaRecipesScreen");
            LoadContents(this, node);
            m_craftingRecipeWidget = Children.Find<IECraftingRecipeWidget>("CraftingRecipe");
            m_smeltingRecipeWidget = Children.Find<IESmeltingRecipeWidget>("SmeltingRecipe");
            m_one2OneRecipeWidget = Children.Find<One2OneRecipeWidget>("One2OneRecipe");
            m_siftingRecipeWidget = Children.Find<SiftingRecipeWidget>("SiftingRecipe");
            m_cokingRecipeWidget = Children.Find<CokingRecipeWidget>("CokingRecipe");
            m_prevRecipeButton = Children.Find<ButtonWidget>("PreviousRecipe");
            m_nextRecipeButton = Children.Find<ButtonWidget>("NextRecipe");
        }

        public override void Enter(object[] parameters) {
            int value = (int)parameters[0];
            RecipesStack.Clear();
            m_craftingRecipes.Clear();
            m_craftingRecipes.AddRange(IECraftingRecipesManager.SearchForRecipe(value));
            m_recipeIndex = 0;
        }

        public void SwitchToNewRecipe(List<CraftingRecipe> recipes, int index) {//切换至新配方，并且向配方堆栈里将旧配方压入
            RecipesStack.Push((m_craftingRecipes, m_recipeIndex));
            m_craftingRecipes = recipes;
            m_recipeIndex = index;
        }

        public void SwitchToPreviousRecipe() {//将配方堆栈内的上级配方弹出
            var previousRecipe = RecipesStack.Pop();
            m_craftingRecipes = previousRecipe.Recipes;
            m_recipeIndex = previousRecipe.Index;
        }

        public override void Update() {
            if (m_recipeIndex < m_craftingRecipes.Count) {
                CraftingRecipe craftingRecipe = m_craftingRecipes[m_recipeIndex];
                if (craftingRecipe.RequiredHeatLevel == 0f) {
                    m_craftingRecipeWidget.Recipe = craftingRecipe;
                    m_craftingRecipeWidget.NameSuffix = string.Format(LanguageControl.GetContentWidgets(nameof(RecipaediaRecipesScreen), 1), m_recipeIndex + 1);
                    m_craftingRecipeWidget.IsVisible = true;
                    m_smeltingRecipeWidget.IsVisible = false;
                    m_one2OneRecipeWidget.IsVisible = false;
                    m_siftingRecipeWidget.IsVisible = false;
                    m_cokingRecipeWidget.IsVisible = false;
                }
                else if(craftingRecipe.RequiredHeatLevel > 0f) {
                    m_smeltingRecipeWidget.Recipe = craftingRecipe;
                    m_smeltingRecipeWidget.NameSuffix = string.Format(LanguageControl.GetContentWidgets(nameof(RecipaediaRecipesScreen), 1), m_recipeIndex + 1);
                    m_smeltingRecipeWidget.IsVisible = true;
                    m_craftingRecipeWidget.IsVisible = false;
                    m_one2OneRecipeWidget.IsVisible = false;
                    m_siftingRecipeWidget.IsVisible = false;
                    m_cokingRecipeWidget.IsVisible = false;
                }
                else if(craftingRecipe.RequiredHeatLevel is -1f or -2f or -3f or -4f or -5f) {
                    m_one2OneRecipeWidget.Recipe = craftingRecipe;
                    m_one2OneRecipeWidget.NameSuffix = string.Format(LanguageControl.GetContentWidgets(nameof(RecipaediaRecipesScreen), 1), m_recipeIndex + 1);
                    m_one2OneRecipeWidget.IsVisible = true;
                    m_craftingRecipeWidget.IsVisible = false;
                    m_smeltingRecipeWidget.IsVisible = false;
                    m_siftingRecipeWidget.IsVisible = false;
                    m_cokingRecipeWidget.IsVisible = false;
                }
                else if(craftingRecipe.RequiredHeatLevel is -6f) {
                    m_siftingRecipeWidget.Recipe = craftingRecipe;
                    m_siftingRecipeWidget.NameSuffix = string.Format(LanguageControl.GetContentWidgets(nameof(RecipaediaRecipesScreen), 1), m_recipeIndex + 1);
                    m_siftingRecipeWidget.IsVisible = true;
                    m_craftingRecipeWidget.IsVisible = false;
                    m_smeltingRecipeWidget.IsVisible = false;
                    m_one2OneRecipeWidget.IsVisible = false;
                    m_cokingRecipeWidget.IsVisible = false;
                }
                else if(craftingRecipe.RequiredHeatLevel is -7f) {
                    m_cokingRecipeWidget.Recipe = craftingRecipe;
                    m_cokingRecipeWidget.NameSuffix = string.Format(LanguageControl.GetContentWidgets(nameof(RecipaediaRecipesScreen), 1), m_recipeIndex + 1);
                    m_cokingRecipeWidget.IsVisible = true;
                    m_craftingRecipeWidget.IsVisible = false;
                    m_smeltingRecipeWidget.IsVisible = false;
                    m_one2OneRecipeWidget.IsVisible = false;
                    m_siftingRecipeWidget.IsVisible = false;
                }
            }
            m_prevRecipeButton.IsEnabled = m_recipeIndex > 0;
            m_nextRecipeButton.IsEnabled = m_recipeIndex < m_craftingRecipes.Count - 1;
            if (m_prevRecipeButton.IsClicked) {
                m_recipeIndex = MathUtils.Max(m_recipeIndex - 1, 0);
            }
            if (m_nextRecipeButton.IsClicked) {
                m_recipeIndex = MathUtils.Min(m_recipeIndex + 1, m_craftingRecipes.Count - 1);
            }

            Children.Find<ButtonWidget>("PreviousRecipeInStack").IsEnabled = RecipesStack.Count > 0;

            if (Input.Back
                || Input.Cancel
                || Children.Find<ButtonWidget>("PreviousRecipeInStack").IsClicked) {
                if (RecipesStack.Count > 0) {
                    SwitchToPreviousRecipe();
                }
                else {
                    ScreensManager.SwitchScreen(ScreensManager.PreviousScreen);
                }
            }
            if (Children.Find<ButtonWidget>("TopBar.Back").IsClicked) {
                ScreensManager.SwitchScreen(ScreensManager.PreviousScreen);
            }
        }
    }
}